package com.ourhours.message.utils;

import com.ourhours.message.exception.MessageException;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.HtmlEmail;

import java.net.URL;

public class EmailUtils {
    final private static String ACCOUNT = "auto-sender@ourhours.net";
    final private static String PASS = "Thce365";
    final private static String HOST = "smtp.exmail.qq.com";
    final private static String ACCOUNTNAME = "全时系统";

    public static void sendEmail(String[] toAddress, String title, String content, String account, String pass
            , String host, String accountName, String[] localAttachment, String[] networkAttachment) {
        try {
            if (account == null || account.isEmpty()) {
                account = ACCOUNT;
            }
            if (pass == null || pass.isEmpty()) {
                pass = PASS;
            }
            if (host == null || host.isEmpty()) {
                host = HOST;
            }
            if (accountName == null || accountName.isEmpty()) {
                accountName = ACCOUNTNAME;
            }
            if (toAddress == null || toAddress.length == 0) {
                throw new MessageException("目标邮箱不能为空！");
            }
            HtmlEmail htmlEmail = new HtmlEmail();
            // 你的邮件服务器的地址
            htmlEmail.setHostName(host);
            // 如果你的邮件服务器设置了密码，请输入密码，否则此语句可以忽略
            htmlEmail.setAuthentication(account, pass);
            for (int i = 0; i < toAddress.length; i++) {
                //设置收件人，如果想添加多个收件人，将此语句多写几次即可。其中参数1，代表收件人邮件地址；
                htmlEmail.addTo(toAddress[i]);
            }
            // 发件人 参数2，用于收件人收到邮件时看到的收件人姓名
            htmlEmail.setFrom(account, accountName);
            // 标题
            htmlEmail.setSubject(title);
            // 内容
            htmlEmail.setMsg(content);
            // 附件
            if ((localAttachment != null && localAttachment.length > 0) || (networkAttachment != null && networkAttachment.length > 0)) {
                if (networkAttachment != null && networkAttachment.length > 0) {
                    for (int i = 0; i < networkAttachment.length; i++) {
                        EmailAttachment attachment = new EmailAttachment();
                        //"http://www.apache.org/images/asf_logo_wide.gif"
                        attachment.setURL(new URL(networkAttachment[i]));
                        htmlEmail.attach(attachment);
                    }
                }
                if (localAttachment != null && localAttachment.length > 0) {
                    for (int i = 0; i < localAttachment.length; i++) {
                        EmailAttachment attachment = new EmailAttachment();
                        attachment.setPath(localAttachment[i]);
                        htmlEmail.attach(attachment);
                    }
                }
            }
            htmlEmail.send();//发送邮件
            // html格式
//            htmlEmail.setHtmlMsg(testEditor);//testEditor 变量包含html内容
        }catch (Exception e){
            throw new MessageException(e.getMessage());
        }
    }

    private static void sendEmail(String[] toAddress, String title, String content) {
        sendEmail(toAddress, title, content, null, null, null, null, null, null);
    }
}
